<script lang="ts" setup>
  import { useECharts } from '@/hooks/web/useECharts';
  import { onMounted, useTemplateRef, ref } from 'vue';
  import { getAllType } from '@/server/home.js';
  import type { Ref } from 'vue';

  const info = ref([]);
  const chartRef = useTemplateRef<HTMLDivElement | null>('chart-ref');
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);

  async function loadData() {
    const res = await getAllType();
    if (res.code == 200) {
      if (res.data && res.data.length) {
        info.value = res.data.map((v: { total: any; name: any }) => ({ value: v.total, name: v.name }));
      }
    }
  }

  onMounted(async () => {
    await loadData();
    setOptions({
      tooltip: {
        trigger: 'item',
      },
      legend: {
        type: 'scroll',
        orient: 'vertical',
        right: 0,

      },
      series: [
        {
          name: '商品总数',
          type: 'pie',
          radius: ['45%', '80%'],
          center:['40%','50%'],
          avoidLabelOverlap: false,
          itemStyle: {
            borderRadius: 0,
            borderColor: '#fff',
            borderWidth: 2,
          },
          label: {
            show: false,
            position: 'center',
          },
          emphasis: {
            label: {
              show: true,
              fontSize: '30',
              fontWeight: 'bold',
            },
          },
          labelLine: {
            show: false,
          },
          data: [...info.value],
          minAngle:8
        },
      ],
    });
  });
</script>

<template>
  <div ref="chart-ref" class="chart-ref" />
</template>

<style scoped lang="scss">
  .chart-ref {
    width: 100%;
    height: 54vh;
  }
</style>
